import Db from './untils/localStore'

const person = {
  state: {
   tab: 0,
    userInfo: {}
  },
  mutations: {
    changePersonTab (state, active) {
      state.tab = active
    },
    getPersonUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    changePersonTab ({state, commit}, active) {
      //每次读取的时候，更新到localstore里保存数据
      Db.save({key: "tab", val: active}).then(() => {
        commit('changePersonTab', active)
      })
    },
    updatePersonTab ({state, commit}) {

      // 获取到数据，进行更新
      Db.get('tab').then((res) => {
        if( state.tab != res) {
          commit('changePersonTab', res)
        }
      })
    },

    saveUserInfo ({state, commit}, Info) {
      // 保存token
      Info['token'] &&  Db.save({key: 'token', val: Info['token']})
      //保存用户信息
       Db.save({key: 'userInfo', val: Info.userinfo}).then(() =>{
         commit('getPersonUserInfo', Info.userinfo)
       })
    },
    updateUserInfo ({state, commit}) {
      // 更新用户信息 如果需要json格式化，在第二参数中传入true
      Db.get('userInfo', true).then(res => {
        commit('getPersonUserInfo', res)
      })
    }
  }
}


export default person
